import { MockMethod } from 'vite-plugin-mock'
import { MenuType } from '@/model/user-role-model'

// 模拟权限数据
const permissions = [
  {
    id: 1,
    name: '系统管理',
    url: null,
    method: null,
    parent_id: null,
    level: 1,
    order: 1,
    icon: 'Setting',
    is_menu: MenuType.MENU,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 2,
    name: '用户管理',
    url: '/system/users',
    method: 'GET',
    parent_id: 1,
    level: 2,
    order: 1,
    icon: 'User',
    is_menu: MenuType.MENU,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 3,
    name: '角色管理',
    url: '/system/roles',
    method: 'GET',
    parent_id: 1,
    level: 2,
    order: 2,
    icon: 'UserFilled',
    is_menu: MenuType.MENU,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 4,
    name: '权限管理',
    url: '/system/permissions',
    method: 'GET',
    parent_id: 1,
    level: 2,
    order: 3,
    icon: 'Lock',
    is_menu: MenuType.MENU,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 5,
    name: '新增用户',
    url: '/api/users',
    method: 'POST',
    parent_id: 2,
    level: 3,
    order: 1,
    icon: null,
    is_menu: MenuType.BUTTON,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 6,
    name: '编辑用户',
    url: '/api/users/:id',
    method: 'PUT',
    parent_id: 2,
    level: 3,
    order: 2,
    icon: null,
    is_menu: MenuType.BUTTON,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 7,
    name: '删除用户',
    url: '/api/users/:id',
    method: 'DELETE',
    parent_id: 2,
    level: 3,
    order: 3,
    icon: null,
    is_menu: MenuType.BUTTON,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 8,
    name: '重置密码',
    url: '/api/users/:id/reset-password',
    method: 'POST',
    parent_id: 2,
    level: 3,
    order: 4,
    icon: null,
    is_menu: MenuType.BUTTON,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 9,
    name: '新增角色',
    url: '/api/roles',
    method: 'POST',
    parent_id: 3,
    level: 3,
    order: 1,
    icon: null,
    is_menu: MenuType.BUTTON,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  },
  {
    id: 10,
    name: '编辑角色',
    url: '/api/roles/:id',
    method: 'PUT',
    parent_id: 3,
    level: 3,
    order: 2,
    icon: null,
    is_menu: MenuType.BUTTON,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00'
  }
]

// 生成权限树
const generatePermissionTree = () => {
  const tree = []
  const map = {}

  // 先构建映射
  permissions.forEach(permission => {
    map[permission.id] = { ...permission, children: [] }
  })

  // 构建树
  permissions.forEach(permission => {
    const node = map[permission.id]
    if (permission.parent_id === null) {
      tree.push(node)
    } else {
      if (map[permission.parent_id]) {
        map[permission.parent_id].children.push(node)
      }
    }
  })

  return tree
}

export default [
  // 获取权限列表
  {
    url: '/permissions',
    method: 'get',
    response: () => {
      return {
        code: 0,
        data: permissions,
        total: permissions.length,
        errorMsg: ''
      }
    }
  },
  
  // 获取权限树
  {
    url: '/permissions/tree',
    method: 'get',
    response: () => {
      return {
        code: 0,
        data: generatePermissionTree(),
        errorMsg: ''
      }
    }
  },
  
  // 获取单个权限详情
  {
    url: '/permissions/:id',
    method: 'get',
    response: (opt: any) => {
      const { id } = opt.query
      const permission = permissions.find(p => p.id === Number(id))
      
      if (permission) {
        return {
          code: 0,
          data: permission,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '权限不存在'
        }
      }
    }
  },
  
  // 创建权限
  {
    url: '/permissions',
    method: 'post',
    response: (opt: any) => {
      const { body } = opt
      const newId = Math.max(...permissions.map(p => p.id)) + 1
      const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
      
      const newPermission = {
        ...body,
        id: newId,
        created_at: now,
        updated_at: now
      }
      
      permissions.push(newPermission)
      
      return {
        code: 0,
        data: newPermission,
        errorMsg: ''
      }
    }
  },
  
  // 更新权限
  {
    url: '/permissions/:id',
    method: 'put',
    response: (opt: any) => {
      const { id } = opt.query
      const { body } = opt
      const index = permissions.findIndex(p => p.id === Number(id))
      
      if (index !== -1) {
        const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
        const updatedPermission = {
          ...permissions[index],
          ...body,
          updated_at: now
        }
        
        permissions[index] = updatedPermission
        
        return {
          code: 0,
          data: updatedPermission,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '权限不存在'
        }
      }
    }
  },
  
  // 删除权限
  {
    url: '/permissions/:id',
    method: 'delete',
    response: (opt: any) => {
      const { id } = opt.query
      const index = permissions.findIndex(p => p.id === Number(id))
      
      if (index !== -1) {
        permissions.splice(index, 1)
        
        return {
          code: 0,
          data: null,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '权限不存在'
        }
      }
    }
  },
  
  // 获取用户菜单权限
  {
    url: '/auth/menu-permissions',
    method: 'get',
    response: (opt: any) => {
      // 从请求头部中获取授权令牌，可以根据令牌确定用户身份
      // 这里为了简化示例，我们将返回固定的权限数据
      
      // 返回多种权限类型数组，模拟用户有多个权限角色
      return {
        code: 0,
        data: [
          { id: 10, name: '系统管理员', type: 'role', description: '系统管理员角色' },
          { id: 11, name: '服务器管理员', type: 'role', description: '服务器管理员角色' },
          { id: 12, name: '项目管理员', type: 'role', description: '项目管理员角色' },
          { id: 13, name: '部署管理员', type: 'role', description: '部署管理员角色' }
        ],
        errorMsg: ''
      };
      
      // 注意：下面的代码不会执行，因为我们已经返回了数据
      // 这里保留是为了演示另一种可能的返回格式（直接返回菜单路由）
      const userMenuRoutes = [
        {
          path: '/dashboard',
          component: 'Layout',
          children: [
            {
              path: '',
              name: 'Dashboard',
              component: '@/views/dashboard/Dashboard.vue',
              meta: { title: '首页', icon: 'house' }
            }
          ]
        },
        {
          path: '/project',
          component: 'Layout',
          redirect: '/project/list',
          meta: { title: '项目管理', icon: 'document' },
          children: [
            {
              path: 'list',
              name: 'ProjectList',
              component: '@/views/project/List.vue',
              meta: { title: '项目列表', icon: 'list' }
            }
          ]
        },
        {
          path: '/server',
          component: 'Layout',
          redirect: '/server/list',
          meta: { title: '服务器管理', icon: 'cpu' },
          children: [
            {
              path: 'list',
              name: 'ServerList',
              component: '@/views/server/List.vue',
              meta: { title: '服务器列表', icon: 'list' }
            }
          ]
        },
        {
          path: '/deploy',
          component: 'Layout',
          redirect: '/deploy/list',
          meta: { title: '部署管理', icon: 'upload' },
          children: [
            {
              path: 'list',
              name: 'DeployList',
              component: '@/views/deploy/List.vue',
              meta: { title: '部署列表', icon: 'list' }
            }
          ]
        },
        {
          path: '/system',
          component: 'Layout',
          redirect: '/system/users',
          meta: { title: '系统管理', icon: 'setting' },
          children: [
            {
              path: 'users',
              name: 'UserManagement',
              component: '@/views/user/List.vue',
              meta: { title: '用户管理', icon: 'user' }
            },
            {
              path: 'roles',
              name: 'RoleManagement',
              component: '@/views/role/List.vue',
              meta: { title: '角色管理', icon: 'user' }
            },
            {
              path: 'permissions',
              name: 'PermissionManagement',
              component: '@/views/system/permission/List.vue',
              meta: { title: '权限管理', icon: 'lock' }
            }
          ]
        }
      ];
      
      return {
        code: 0,
        data: userMenuRoutes,
        errorMsg: ''
      };
    }
  }
] as MockMethod[]
